import { computed, ref, unref, watch } from 'vue'
import { FormInstance } from 'element-plus';
import request from '/@/utils/request';
interface FormlUrl {
    query: string,
    delete: string,
    create: string,
    update: string
}
function useForm(url: FormlUrl, { transform }: any) {
    const form = ref({}), type = ref('create')

    // 请求上传的表单 (为了方便处理表单绑定的表单与请求所需表单存在差异的情况)
    const payload = computed(() => {
        return transform ? transform(unref(form)) : form.value
    })

    /**
     * 表单提交
     * @param formEl 表单ref
     * @returns 
     */
    function onSubmit(formEl: FormInstance | undefined) {
        if (!formEl) return
        formEl.validate((valid, fields) => {
            if (valid) {
                if(type.value === 'create') {
                    request({ url: url.create, method: 'post', data: payload}).then(res => {
                        if(res) {
        
                        }
                    })
                } else {
                    request({ url: url.update, method: 'put', data: payload}).then(res => {
                        if(res) {
                            
                        }
                    })
                }
            } else {
                console.log('校验失败')
            }
        })
    }
}